#include "InsertDatabase.h"

InsertDataBase::InsertDataBase()
{

}

InsertDataBase::~InsertDataBase()
{

}


void InsertDataBase::insertSpell(QString name, int deg_min, int deg_rand, QString str_D, int id_effect, int time_inc, int conso_mana)
{

    QSqlQuery query;
    query.exec("SELECT max(id) FROM SPELLS");
    int id = 1;
    if(query.next())
        id = query.value(0).toInt()+1;
    query.prepare("INSERT INTO SPELLS (id, name, deg_min, deg_random, nb_D, id_effect, time_inc, conso_mana) "
                   "VALUES (:id, :name, :deg_min, :deg_rand, :str_D, :id_effect, :time_inc, :conso_mana)");
    query.bindValue(0, QVariant(id));
    query.bindValue(1, QVariant(name));
    query.bindValue(2, QVariant(deg_min));
    query.bindValue(3, QVariant(deg_rand));
    query.bindValue(4, QVariant(str_D));
    query.bindValue(5, QVariant(id_effect));
    query.bindValue(6, QVariant(time_inc));
    query.bindValue(7, QVariant(conso_mana));
    query.exec();
    if(query.lastError().isValid())
        QMessageBox::information(0, "Sauvegarde impossible", "Impossible de sauvegarder le sort :"+name+"\n"+query.lastError().text());
}

void InsertDataBase::insertElement(QString name, QString description)
{
    QSqlQuery query;
    query.exec("SELECT max(id) FROM elements");
    int id = 1;
    if(query.next())
        id = query.value(0).toInt()+1;
    query.prepare("INSERT INTO elements (id, name, description) "
                   "VALUES (:id, :name, :description)");
    query.bindValue(0, QVariant(id));
    query.bindValue(1, QVariant(name));
    query.bindValue(2, QVariant(description));
    query.exec();
    if(query.lastError().isValid())
        QMessageBox::information(0, "Sauvegarde impossible", "Impossible de sauvegarder l'élément :"+name+"\n"+query.lastError().text());
}

void InsertDataBase::insertEffect(QString name, QString description)
{
    QSqlQuery query;
    query.exec("SELECT max(id) FROM effects");
    int id = 1;
    if(query.next())
        id = query.value(0).toInt()+1;
    query.prepare("INSERT INTO effects (id, name, description) "
                   "VALUES (:id, :name, :description)");
    query.bindValue(0, QVariant(id));
    query.bindValue(1, QVariant(name));
    query.bindValue(2, QVariant(description));
    query.exec();
    if(query.lastError().isValid())
        QMessageBox::information(0, "Sauvegarde impossible", "Impossible de sauvegarder l'effet :"+name+"\n"+query.lastError().text());
}

void InsertDataBase::insertClass(QString name, double hp, double mana)
{
    QSqlQuery query;
    query.exec("SELECT max(id) FROM class");
    int id = 1;
    if(query.next())
        id = query.value(0).toInt()+1;
    query.prepare("INSERT INTO class (id, coef_hp, coef_mana, name) "
                   "VALUES (:id, :coef_hp, :coef_mana, :name)");
    query.bindValue(0, QVariant(id));
    query.bindValue(1, QVariant(hp));
    query.bindValue(2, QVariant(mana));
    query.bindValue(3, QVariant(name));
    query.exec();
    if(query.lastError().isValid())
        QMessageBox::information(0, "Sauvegarde impossible", "Impossible de sauvegarder la classe :"+name+"\n"+query.lastError().text());

}

void InsertDataBase::insertRace(QString name, double hp, double mana)
{
    QSqlQuery query;
    query.exec("SELECT max(id) FROM race");
    int id = 1;
    if(query.next())
        id = query.value(0).toInt()+1;
    query.prepare("INSERT INTO race (id, coef_hp, coef_mana, name) "
                   "VALUES (:id, :coef_hp, :coef_mana, :name)");
    query.bindValue(0, QVariant(id));
    query.bindValue(1, QVariant(hp));
    query.bindValue(2, QVariant(mana));
    query.bindValue(3, QVariant(name));
    query.exec();
    if(query.lastError().isValid())
        QMessageBox::information(0, "Sauvegarde impossible", "Impossible de sauvegarder la classe :"+name+"\n"+query.lastError().text());

}
